package com.qulai.web.controller.basic;

import com.qulai.amazon_us.domain.basic.Supplier;
import com.qulai.amazon_us.mapper.basic.ImageMapper;
import com.qulai.amazon_us.mapper.basic.ProductMapper;
import com.qulai.amazon_us.service.basic.ISupplierService;
import com.qulai.common.annotation.Log;
import com.qulai.common.core.controller.BaseController;
import com.qulai.common.core.domain.AjaxResult;
import com.qulai.common.core.page.TableDataInfo;
import com.qulai.common.enums.BusinessType;
import com.qulai.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 货源供应商Controller
 *
 * @author hunan
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/basic/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ImageMapper imageMapper;

    /**
     * 查询货源供应商列表
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier) {
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        if (list != null && !list.isEmpty()) {
            for (Supplier tmp : list) {
                if (Objects.equals(tmp.getPlatform(), "DB")) {
                    String link = String.format("https://www.doba.com/dropshipping-supplier/%s/products", tmp.getCode());
                    tmp.setLink(link);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出货源供应商列表
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:export')")
    @Log(title = "货源供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supplier supplier) {
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        util.exportExcel(response, list, "货源供应商数据");
    }

    /**
     * 获取货源供应商详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(supplierService.selectSupplierById(id));
    }

    /**
     * 新增货源供应商
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:add')")
    @Log(title = "货源供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Supplier supplier) {
        supplier.setUpdateBy(getUsername());
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改货源供应商
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:edit')")
    @Log(title = "货源供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Supplier supplier) {
        supplier.setUpdateBy(getUsername());
        if (supplier.getStatus() == 0) {
            //需要去禁用各自商品
            productMapper.updateOfflineProduct(String.format("supplier_code = '%s'", supplier.getCode()), 11, supplier.getRemark());
            imageMapper.updateOfflineProduct(String.format(" basic_id in ( select id from basic_product where supplier_code = '%s' )", supplier.getCode()), 11);
        } else {
            //去启用,也只能是 状态为待校验
            productMapper.updateOfflineProduct(String.format("supplier_code = '%s'", supplier.getCode()), 0, supplier.getRemark());
            imageMapper.updateOfflineProduct(String.format(" basic_id in ( select id from basic_product where supplier_code = '%s' )", supplier.getCode()), 0);
        }

        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除货源供应商
     */
    @PreAuthorize("@ss.hasPermi('basic:supplier:remove')")
    @Log(title = "货源供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(supplierService.deleteSupplierByIds(ids));
    }
}
